package com.joysuch.wwyt.app.service.impl;

import com.joysuch.wwyt.app.service.AppInvestigateService;
import com.joysuch.wwyt.bp.bean.investigate.BpInvestigateAppTaskDto;
import com.joysuch.wwyt.bp.bean.investigate.BpInvestigateRecordQueryBean;
import com.joysuch.wwyt.bp.entity.investigate.BpInvestigatePlan;
import com.joysuch.wwyt.bp.entity.investigate.BpInvestigateRecord;
import com.joysuch.wwyt.bp.repository.BpInvestigatePlanRepository;
import com.joysuch.wwyt.bp.repository.BpInvestigateRecordRepository;
import com.joysuch.wwyt.bp.service.BpInvestigatePlanService;
import com.joysuch.wwyt.bp.service.BpInvestigateRecordService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.support.Context;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @Author ZhangDong
 * @Date 2020/6/16 15:30
 */
@Service
@Slf4j
public class AppInvestigateServiceImpl implements AppInvestigateService {

    @Autowired
    private BpInvestigatePlanRepository planRepository;
    @Autowired
    private BpInvestigatePlanService planService;
    @Autowired
    private BpInvestigateRecordService recordService;
    @Autowired
    private BpInvestigateRecordRepository recordRepository;

    @Override
    public ResultBean planList() {
        List<BpInvestigateAppTaskDto> taskDtoList = planService.appNeedDo();
        return ResultBean.success(taskDtoList);

//        Long currentUserId = Context.getCurrentUserId();
//        //当天如果有排查记录，不再推送
//        List<BpInvestigatePlan> list = planRepository.findCheckPlan(currentUserId, LocalDate.now());
//        for (BpInvestigatePlan plan : list) {
//            Long planId = plan.getId();
//            plan = (BpInvestigatePlan) planService.getById(planId, 1).getData();
//            BpInvestigateRecord record = recordRepository.findByCheckPlanIdAndRecordUserId(planId, currentUserId);
//            plan.setLatestCheckDate(record == null ? null : record.getCheckFinishDate());
//        }
//        return ResultBean.wrapSuccessfulResult(list);
    }

    @Override
    public ResultBean recordList(BpInvestigateRecordQueryBean bean) {
        return recordService.page(bean, 1);
    }

    @Override
    public ResultBean getById(Long id) {
        return recordService.getById(id, 1);
    }

    @Override
    public ResultBean planGet(Long id) {
        Assert.notNull(id, "id cannot be null");
        return planService.getById(id, 1);
    }
    @Override
    public ResultBean getInfo(Long id) {
        Assert.notNull(id, "id cannot be null");
        return planService.getPlanById(id, 1);
    }
}
